<?php
/*
 * (C) Copyright 2013 Torsten Gorath (http://www.blauesdach.de/).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Contributors:
 *     Torsten Gorath
 */
require_once $basedir.'/inc/permissions.php';
require_once $basedir.'/inc/role.php';

$permissions = Permission::getAll();
$roles = Role::getAll();
?>
<script language="JavaScript" type="text/javascript">
    $(function() {
        function init() {
            $("#add_permission").css('display','none');
        }
        
        $("#role_form_block").dialog({
            autoOpen: false,
            modal: true,
            show: {
                effect: "explode",
                duration: 250
            },
            hide: {
                effect: "explode",
                duration: 250
            }
        })
        
        $("input[name='abord']").click(function() {
            $("#role_form_block").dialog("close");
        });
        
        $('#role_form').ajaxForm({
            beforeSubmit: validate
        })
        .submit(function() {
            $("#role_form_block").dialog("close");
            var form_data = $(this).serializeArray();
            form_data.push({
                name:'save',
                value:''
            });
            $.ajax({
                type:"POST",
                url:"json.php?page=<? echo $menuid; ?>",
                data:form_data,
                dataType:"JSON",
                success:function(data) {
                    if(data.error) {
                        alert(data.msg);
                    } else {
                        $("#role_form").find("input").val("");
                        $("#role_form").find("textarea").val("");
                    }
                }
            });
            return false;
        });
        
        $('a[href*="add_role"]').click(function() {
            $("#role_form_block").dialog("open");
            $("#role_form").find("input[name='id']").val("");
            $("#role_form").find("input[name*='name']").val("");
            $("#role_form").find("textareaname='description']").val("");
            $("#permissions_table > tbody").empty();
        });

        $('a[href*="edit_role"]').click(function() {
            var id = $(this).attr('href').split("_")[2];
            var data = {
                "get": "",
                "id": id
            };
            $.ajax({
                "type":"POST",
                "url":"json.php?page=<? echo $menuid; ?>",
                "data":data,
                "dataType":"JSON",
                "success":function(data) {
                    if(data.error) {
                        alert(data.msg);
                    } else {
                        $("#role_form_block").dialog("open");
                        $("#role_form").find("input[name='id']").val(data.id);
                        $("#role_form").find("input[name*='name']").val(data.name);
                        $("#role_form").find("textarea[name='description']").val(data.description);
                        $("#permissions_table > tbody").empty();
                        var count = 1;
                        for(var permission in data.permissions) {
                            $("#permissions_table > tbody").append($("<tr/>")
                                .append($("<td>").html(count))
                                .append($("<td/>").html(permission)
                                .append($("<td/>").append($("<a href='#delete/>").html("delete")))
                            ));
                        }
                    }
                }
            });
            $("input[name='id']").val(id);
        });
        
        $("input[name='add_permission']").click(function() {
            $("#add_permission").css('display', 'block');
        });
        
        init();
    })
</script>
<div id='role_form_block'>
    <form id='role_form'>
        <input type='hidden' name='id' value="" />
        <div>
            <div class='inline-block label'>Name:</div>
            <div class='inline-block'>
                <input type='text' name='name_require' value='' />
            </div>
        </div>
        <div>
            <div>
                <div class='inline-block label'>Bechreibung:</div>
                <div class='inline-block'>
                    <textarea width='100%' height='10' name='description'></textarea>
                </div>
            </div>
        </div>
        <div id='permissions'>
            <table id='permissions_table'>
                <thead>
                    <tr>
                        <th><a href='#add_permission'>add</a></th>
                        <th>Name</th>
                        <th></th>
                        <th></th>
                    </tr>
                </thead>
                <tbody></tbody>
            </table>
        </div>
        <div id='add_permission'>
            <select name='new_permission'>
                <option value="" selected="true"></option>
<?php
for($i=0; $i<count($permissions); $i++) {
    $permission = $permissions[$i];
    echo "<option value='".$permission->getId()."'>".$permission->getName()."</option>";
}
?>
            </select><br />
            <input type='button' name='save_permission' value='Hinzuf&uuml;gen' />
            <input type='button' name='abord_permission' value='Abbrechen' />
        </div>
        <div>
            <input type='submit' name='save' value='Speichern' />
            <input type='button' name='abord' value='Abbrechen' />
        </div>
    </form>
</div>
<div>
    <table>
        <thead>
            <tr>
                <th><a href="#add_role">new</a></th>
                <th>Rolle</th>
                <th>Beschreibung</th>
                <th>Berechtigungen</th>
                <th></th>
                <th></th>
            </tr>
        </thead>
        <tbody id="overview">
<?php
for($i=0; $i<count($roles); $i++) {
    $role = $roles[$i];
    $permissionsPrint = "";
    $perms = $role->getPermissions();
    foreach($perms as $key=>$value) {
        $permissionsPrint .= $key."<br />";
    }
?>
            <tr>
                <td><? echo $i+1; ?></td>
                <td><? echo $role->getName(); ?></td>
                <td><? echo $role->getDescription(); ?></td>
                <td><? echo $permissionsPrint; ?></td>
                <td><a href="#edit_role_<? echo $role->getId(); ?>">edit</a></td>
                <td><a href="#delete_role_<? echo $role->getId(); ?>">delete</a></td>
            </tr>
<?php
}
?>
        </tbody>
    </table>
</div>